// 部门
import { defineStore } from 'pinia'
import { storage } from '../utils'
export default defineStore('deptStore', {
  state: () => ({
    dept: [] as any[],
    deptList: JSON.parse(storage.get('CURRENT_DEPTLIST', '[]')) as any[],
  }),
  actions: {
    loadState() {
      const data = storage.get('CURRENT_DEPT', []);
      this.$patch({ dept: JSON.parse(data) });
    },

    setAllState(newState: Partial<any[]>) {
      storage.set('CURRENT_DEPT', JSON.stringify(newState))
      this.$patch({ dept: newState });
    },
    getAllState(): any[] {
      this.loadState();
      return this.dept;
    },
    loadListState() {
      const data = storage.get('CURRENT_DEPTLIST', '[]');
      this.$patch({ deptList: JSON.parse(data) });
    },

    setAllListState(newState: Partial<any[]>) {
      storage.set('CURRENT_DEPTLIST', JSON.stringify(newState))
      this.$patch({ deptList: newState });
    },
    getAllListState(): any[] {
      this.loadListState();
      return this.deptList;
    },
    getDeptById(id: number) {
      this.loadListState();
      return this.deptList?.find((item) => item.deptId === id);
    },
  },
})
